//index.js
//获取应用实例
const app = getApp()

// const url = "http://192.168.6.5:3000/"
const url = "http://localhost:3000/"

Page({
  audioCtx: null,
  data: {
    item: 0,
    tab: 0,
    personalized: [],
    playlist: [],
    personalizedid: 0,
    state: 'paused',
    playIndex: 0,
    play: {
      id: 0,
      currentTime: '00:00',
      duration: '00:00',
      percent: 0,
      title: '',
      singer: '',
      coverImgUrl: '',
    }
  },
  changeItem: function(e) {
    this.setData({
      item: e.target.dataset.item
    })
  },
  changeTab: function(e) {
    this.setData({
      tab: e.detail.current
    })
  },
  changePage: function(e) {
    this.setData({
      item: e.currentTarget.dataset.page
    })
  },
  play: function() {
    this.audioCtx.play()
    this.setData({
      state: 'running'
    })
  },
  pause: function() {
    this.audioCtx.pause()
    this.setData({
      state: 'paused'
    })
  },
  next: function() {
    var index = this.data.playIndex >= this.data.playlist.length - 1 ? 0 : this.data.playIndex + 1
    this.setMusic(index)
    if (this.data.state === 'running') this.play()
  },
  sliderChange: function(e) {
    var second = e.detail.value * this.audioCtx.duration / 100
    this.audioCtx.seek(second)
  },
  change: function(e) {
    this.setMusic(e.currentTarget.dataset.index)
    this.play()
  },
  changeSheet: function(e) {
    this.setData({
      personalizedid: e.currentTarget.dataset.id
    })
    this.setPlayList(e.currentTarget.dataset.id)
  },
  onLoad: function() {
    var that = this
    wx.request({
      url: url + 'personalized',
      method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
      data: {
        limit: 18
      },
      // header: {}, // 设置请求的 header
      success: function(res) {
        that.setData({
          personalized: res.data.result,
          personalizedid: res.data.result[0].id
        })
        that.setPlayList(res.data.result[0].id)
      }
    })
  },
  onReady: function() {
    this.audioCtx = wx.createInnerAudioContext()
    this.setMusic(0)
    var that = this
    this.audioCtx.onError(function() {
      console.log("播放失败", that.audioCtx.src)
      that.next()
    })
    this.audioCtx.onEnded(function() {
      that.next()
    })
    this.audioCtx.onPlay(function() {})
    this.audioCtx.onTimeUpdate(function() {
      that.setData({
        'play.duration': formatTime(that.audioCtx.duration),
        'play.currentTime': formatTime(that.audioCtx.currentTime),
        'play.percent': that.audioCtx.currentTime / that.audioCtx.duration * 100
      })
    })

    function formatTime(time) {
      var minute = Math.floor(time / 60) % 60;
      var second = Math.floor(time) % 60
      return (minute < 10 ? '0' + minute : minute) + ':' + (second < 10 ? '0' + second : second)
    }
  },
  setMusic: function(index) {
    let that = this
    var music = this.data.playlist[index]
    wx.request({
      url: url + 'song/url',
      data: {
        id: music.id
      },
      success: function(res) {
        that.audioCtx.src = res.data.data[0].url
        const flag = res.data.data[0].url === "" ? false : true
        that.setData({
          playIndex: index,
          'play.title': music.name,
          'play.singer': music.singer,
          'play.coverImgUrl': music.coverImgUrl,
          'play.currentTime': '00:00',
          'play.duration': '00:00',
          'play.percent': 0,
          'play.id': music.id
        })
      }
    })
  },
  setPlayList: function(id) {
    var that = this
    wx.request({
      url: url + 'playlist/detail',
      data: {
        id: id
      },
      method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
      // header: {}, // 设置请求的 header
      success: function(res) {
        let list = []
        res.data.playlist.tracks.forEach(music => {
          list.push({
            id: music.id,
            name: music.name,
            singer: music.ar[0].name,
            coverImgUrl: music.al.picUrl,
            flag: true
          })
        })
        that.setData({
          playlist: list
        })
      }
    })
  }
})